//
//  MainViewController.swift
//  weibo
//
//  Created by chenhongen on 15/10/20.
//  Copyright © 2015年 陈弘根. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        /// 添加所有的子控制器
        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        /// 添加加号
        setupComposeButton()
    }
    
    private func setupComposeButton()
    {
        tabBar.addSubview(composeButton)
        
        let width = UIScreen.mainScreen().bounds.width / CGFloat (childViewControllers.count)
        let height = tabBar.bounds.height
        let rect = CGRect(origin: CGPointZero, size: CGSize(width: width, height: height))
        
        composeButton.frame = CGRectOffset(rect, width * 2, 0)
    }
    
    // MARK: - 添加所有子控制器
    private func addChildViewControllers(){
        
        // 1.获取文件路径
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        // 2.根据文件创建
        let data = NSData(contentsOfFile: path)!
        
        do{
            let dictArr = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
            for dict in dictArr as! [[String: AnyObject]]
            {
                addChildViewController(dict["vcName"] as? String, title: dict["title"] as? String,imageName: dict["imageName"] as? String)
            }
        }catch
        {
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            
            addChildViewController(NullViewController())
            
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
    }
    
    // MARK: - 内部控制方法
    func addChildViewController(childControllerName: String?, title: String?, imageName: String?) {
        
        let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"]
        guard let ns = nameSpace as? String else{
            
            return
        }
        
        guard let vcName = childControllerName else
        {
            CHGLog("控制器名称是nil")
            return
        }
        
        let cls: AnyClass? = NSClassFromString(ns + "." + vcName)
        
        guard let clsType = cls as? UIViewController.Type else{
            print("传入的字符串不能当做UIViewController来使用")
            return
        }
        
        let homeVC = clsType.init()

        
        // 1.添加子控制器
        tabBar.tintColor = UIColor.orangeColor()

        // 1.2设置子控制器的相关属性
         if let iName = imageName
         {
            homeVC.tabBarItem.image = UIImage(named: iName)
            homeVC.tabBarItem.selectedImage = UIImage(named: iName
                + "_highlighted")
        }
        homeVC.title = title
        
        // 1.3给子控制器包装一个导航控制器
        let nav = UINavigationController(rootViewController: homeVC)
        
        // 1.4添加
        addChildViewController(nav)
    }
    
    /// MARK:  懒加载
    private lazy var composeButton:UIButton = {
        let btn = UIButton(imageName: "tabbar_compose_button", backImageName: "tabbar_compose_icon_add")
        btn.addTarget(self, action: Selector("composeBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn;
    }()
    
    @objc private func composeBtnClick()
    {
        CHGLog("")
    }
    

}
